/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.office.sessionRepository;

import edu.office.applicationRepository.*;
import edu.office.entities.School;
import edu.office.entities.Student;
import edu.office.entities.Studenttestpaper;
import edu.office.entities.Subject;
import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;

/**
 * @author Administrator
 */


@Named("courseCollabrativeFilter")
@SessionScoped
public class CourseCollabrativeFilter implements java.io.Serializable {
    @Inject
    SubjectRepository applicationSubjectController;
    @Inject
    SchoolRepository applicationSchoolController;
    @Inject
    StudenttestpaperRepository applicationStudentTestPaper;
    @Inject
    TestpaperRepository applicationTestPaper;
    @Inject
    StudentRepository applicationStudent;
    private HashMap<Subject, Double> getStudentScore(Student student) {
        HashMap<Subject, List<Double>> subjectScoreHashMap = new HashMap<>();
        List<Studenttestpaper> studenttestpaper = applicationStudentTestPaper.find("STUDENT_ID", student.getId()).list();
        for (Studenttestpaper stp : studenttestpaper) {
            String subjectids = applicationTestPaper.find("ID", stp.getTESTPAPERID()).firstResult().getSUBJECTIDS();
            String sbujectString[] = subjectids.split(",");
            for (int i = 0; i < sbujectString.length; i++) {
                Subject subject = applicationSubjectController.find("ID", Integer.valueOf(sbujectString[i])).firstResult();
                if (subjectScoreHashMap.get(subject) == null) {
                    List<Double> score = new LinkedList<>();
                    subjectScoreHashMap.put(subject, score);
                }
                subjectScoreHashMap.get(subject).add(stp.getTESTSCORE());
            }
        }
        HashMap<Subject, Double> studentSubScore = new HashMap<>();
        //把线性表中的多次成绩求平均
        for (Entry<Subject, List<Double>> entry : subjectScoreHashMap.entrySet()) {
            List<Double> score =  entry.getValue();
            int count =score.size();
            double tem = 0;
            for (int i = 0; i < count; i++) {
                tem += score.get(i);
            }
            tem = tem / count;
            studentSubScore.put(entry.getKey(), tem);
        }
        return studentSubScore;
    }
    public void outputMatrix() {
        //把所有学生的对应课程的成绩都打印出来
        List<School> allSchools = applicationSchoolController.listAll();
        for (School school : allSchools) {
            for (Student student : applicationStudent.list("SCHOOL_ID",school.getID())) {
                HashMap<Subject, Double> studentScoreHashMap = getStudentScore(student);
                for (Entry entry : studentScoreHashMap.entrySet()) {
                    System.out.print(((Subject) entry.getKey()).getNAME() + ":" + ((Double) entry.getValue()));
                }
            }
        }
    }
}
